#include<bits/stdc++.h>
using namespace std;
const int N=5005,mod=1e9+7;
int w[N],a[N],cnt[1030][N],vis[N],fac[N],Inv[N],tot[N];
int n,k,u,v,ans,l,x;
vector<int> G[N];
inline int C(int n, int m){ return 1ll*fac[n]*Inv[m]%mod*Inv[n-m]%mod; }
int dfs(int u, int f, int sta){
	vis[u]=1; int ret=1;
	for (int v:G[u]){
		if (v==f || (sta&a[v])!=a[v]) continue;
		ret+=dfs(v,u,sta);
	}
	return ret;
}
inline int fastpow(int x, int y){
	int z=1;
	for (; y; y>>=1,x=1ll*x*x%mod)
		if (y&1) z=1ll*z*x%mod;
	return z;
}
int main(){
	freopen("water.in","r",stdin);
	freopen("water.out","w",stdout);
	scanf("%d%d",&n,&k); fac[0]=Inv[0]=1;
	for (int i=1; i<=k; i++) scanf("%d",&w[i]);
	for (int i=1; i<=n; i++){
		scanf("%d",&l);
		for (; l; l--) scanf("%d",&x),a[i]|=(1<<(x-1));
		Inv[i]=fastpow(fac[i]=1ll*fac[i-1]*i%mod,mod-2);
	}
	for (int i=1; i<n; i++){
		scanf("%d%d",&u,&v);
		G[u].push_back(v),G[v].push_back(u);
	}
	for (int i=1; i<(1<<k); i++){
		for (int j=1; j<=n; j++) vis[j]=0;
		for (int j=1; j<=n; j++){
			if (vis[j] || (i&a[j])!=a[j]) continue;
			int c=dfs(j,0,i);
			for (int t=1; t<=c; t++) cnt[i][t]=(cnt[i][t]+C(c,t))%mod;
		}
	}
	for (int i=0; i<k; i++)
		for (int j=0; j<(1<<k); j++){
			if (j&(1<<i)) continue;
			for (int t=1; t<=n; t++) cnt[j|(1<<i)][t]=(cnt[j|(1<<i)][t]+mod-cnt[j][t])%mod;
		}
	tot[0]=1;
	for (int i=1; i<=n; i++)
		for (int j=0; j<i; j++) tot[i]=(tot[i]+1ll*C(i-1,j)*tot[i-j-1])%mod;
	for (int i=1; i<(1<<k); i++){
		int pc=__builtin_popcount(i);
		for (int j=1; j<=n; j++)
			ans=(ans+1ll*cnt[i][j]*tot[n-j]%mod*w[pc])%mod;
	}
	printf("%d\n",ans);
//	cerr<<ans<<endl;
}
/*
7 5
12 34 56 78 90
1 2
2 1 2
2 2 3
3 1 3 4
1 4
2 1 5
2 3 5
1 2
1 3
2 4
2 5
3 6
3 7
*/
